package com.kaizzi.data.jdl.helper;

import com.kaizzi.data.common.been.TempBeen;
import com.kaizzi.data.common.utils.DateUtil;
import com.kaizzi.data.common.utils.JsonUtil;
import com.kaizzi.data.common.utils.LogicUtil;
import com.kaizzi.data.common.utils.StringUtil;
import com.kaizzi.data.jdl.entity.TeamList;
import com.kaizzi.data.jdl.model.ExcelModel;
import com.kaizzi.data.jdl.service.CrmMemberService;
import com.kaizzi.data.jdl.service.TeamListService;
import com.kaizzi.data.jdl.service.TeamMemberService;
import lombok.Data;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;

/**
 * @author Kzen
 * @Description description
 * @date 2020/9/17
 */
@Component
public class PageData27 {

    @Resource
    private TeamListService teamListService;
    @Resource
    private TeamMemberService teamMemberService;

    public String getData(ExcelModel model){
//        ytd
        List<String> ytdDay = DateUtil.getYtdDay1();
//        week
        List<String> dateList = DateUtil.getDateList(1);
        List<String> transTime = DateUtil.getTransTime(dateList.get(0));

        List<TempBeen> ytdArea = teamListService.getTeatArea(ytdDay.get(0), ytdDay.get(1));
        List<TempBeen> ytdGender = teamMemberService.getTeamGender(ytdDay.get(0), ytdDay.get(1));
        List<TempBeen> ytdAge = teamMemberService.getTeamAge(ytdDay.get(0), ytdDay.get(1));

        long teamArea4 = teamListService.getTeamArea4(ytdDay.get(0), ytdDay.get(1));
        ytdArea.add(getTempBeen(ytdArea,teamArea4));
        ytdArea.sort(Comparator.comparing(TempBeen::getTempCount).reversed());
        //
        List<TempBeen> weekArea = teamListService.getTeatArea(transTime.get(0),transTime.get(1));
        List<TempBeen> weekGender = teamMemberService.getTeamGender(transTime.get(0),transTime.get(1));
        List<TempBeen> weekAge = teamMemberService.getTeamAge(transTime.get(0),transTime.get(1));

        long teamArea41 = teamListService.getTeamArea4(transTime.get(0), transTime.get(1));
        weekArea.add(getTempBeen(weekArea,teamArea41));
        weekArea.sort(Comparator.comparing(TempBeen::getTempCount).reversed());

        //text
        List<String> ytdList = new ArrayList<>();
        List<String> weekList = new ArrayList<>();

        String weekChildTitle = getChildTitle(weekGender);
        String ytdChildTitle = getChildTitle(ytdGender);


        ytdList.add("累计"+getText1(ytdGender));
        ytdList.add("累计"+getText2(ytdAge));
        ytdList.add("累计"+getText3(ytdArea));

        weekList.add("本周"+getText1(weekGender));
        weekList.add("本周"+getText2(weekAge));
        weekList.add("本周"+getText3(weekArea));

        Page27 page27 = new Page27();
        page27.setDataPeriod(DateUtil.getLastWeekDate());
        page27.setTitle(LogicUtil.getTitle(model,27));

        page27.setYtdAge(ytdAge);
        page27.setYtdArea(ytdArea);
        page27.setYtdGender(ytdGender);
        page27.setYtdText(ytdList);
        page27.setYtdTitle("累计"+ytdChildTitle);

        page27.setWeekAge(weekAge);
        page27.setWeekArea(weekArea);
        page27.setWeekGender(weekGender);
        page27.setWeekText(weekList);
        page27.setWeekTitle("本周"+weekChildTitle);

        return JsonUtil.serialize(page27);

    }

    private TempBeen getTempBeen(List<TempBeen> list, long teamArea41) {
        long sum = list.stream().mapToLong(TempBeen::getTempCount).sum();
        TempBeen tempBeen = new TempBeen();
        tempBeen.setTempName("其他");
        tempBeen.setTempCount(teamArea41 - sum);
        return tempBeen;
    }


    private String getChildTitle(List<TempBeen> list){
        String s = StringUtil.pointFormat(list.stream().mapToDouble(TempBeen::getTempCount).sum());
        return "共有'"+s +"位'用户参与一键组队";
    }

    private String getText1(List<TempBeen> list){
        List<String> genderList = new ArrayList<>();
        for (TempBeen been : list) {
            if (been.getTempName().equals("未知")){
                continue;
            }
            genderList.add(StringUtil.decimalFormat(been.getTempCount())+"人为"+been.getTempName()+"用户");
        }
        return "一键组队用户中'"+String.join("、",genderList)+"'";
    }

    private String getText2(List<TempBeen> list){
        if (list == null || list.size()<2){
            return "";
        }
        List<String> ageList = new ArrayList<>();
        for (int i=0;i<2;i++) {
            ageList.add(list.get(i).getTempName());
        }
        return "用户年龄以'"+String.join("、",ageList)+"'两个年龄段居多";
    }

    private String getText3(List<TempBeen> list){
        if (list == null || list.size()<3){
            return "";
        }
        List<String> areaList = new ArrayList<>();
        for (int i=0;i<3;i++) {
            areaList.add(list.get(i).getTempName());
        }
        return "一键组队用户的主要分布地区在'"+String.join("、",areaList)+"'等地";
    }


    @Data
    private static class Page27{
        private String title;
        private String ytdTitle;
        private String weekTitle;
        private List<TempBeen> ytdGender;
        private List<TempBeen> ytdAge;
        private List<TempBeen> ytdArea;
        private List<TempBeen> weekGender;
        private List<TempBeen> weekAge;
        private List<TempBeen> weekArea;
        private String dataPeriod;
        private List<String> ytdText;
        private List<String> weekText;
    }
}
